
# 案例 ：14. 输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
def str_stac(my_str):
    char = 0
    space = 0
    number = 0
    other = 0
    str_dict = {'char':char,'space':space,'number':number,'other':other}

    # 循环这个字符串
    for x in my_str:
        if str(x).isdigit():
            number += 1
            continue
        if str(x).isalpha():
            char += 1
            continue
        if str(x).isspace():
            space += 1
            continue
        other += 1

    # 字典更新
    str_dict['char'] = char
    str_dict['space'] = space
    str_dict['number'] = number
    str_dict['other'] = other
    return str_dict


print(str_stac("sdfsd67823ijrfsd hk f  s#$%^&*hdsfdsfsdf "))

# 15.把原字典的键值对颠倒生成一个新的键值对， 例如： 将该字典；{'A':'a', 'B':'b', 'C':'c'}， 键值对颠倒生成一个新的字典
def reverse_dict(dct):
    new_dict = {}
    # 1. 判断传入数据类型，若不是字典，返回报错 ，isinstance(数据类型,类型)
    if not isinstance(dct,dict):
        return "输入的数据类型不正确"

    # 字典循环 ，items()
    for key,value in dct.items():
        print("key:",key,"==value:",value)
        new_dict[value] = key
    return new_dict

print("="*30)
print(reverse_dict({'A':'a', 'B':'b', 'C':'c'}))
# 16. 字符串s = 'Life is short You need python'。统计这个字符串中每个单词的字母数量.

def statistics_word_nums(s):
    dct = {}
    for x in  str(s).split(" "):
        dct[x] = len(x)
    return dct


print(statistics_word_nums("Life is short You need python"))


